clear
use "anes_timeseries_2016.dta" 
rename V161158x party_id
rename V161310a race_white 
rename V161361x income 
rename V161270 education
rename V162106 muslim_feeling 
rename V162255x muslim_obama
rename V162353 muslim_violent 

rename V161086 therm_c
rename V161087 therm_t

rename V161160 cares_c
rename V161162 honest_c
rename V161163 speaksmind_c


rename V161165 cares_t
rename V161167 honest_t
rename V161168 speaksmind_t

gen terror_worry = 0
replace terror_worry = 1 if V162160 == 5
replace terror_worry = 2 if V162160 == 4
replace terror_worry = 3 if V162160 == 3
replace terror_worry = 4 if V162160 == 2
replace terror_worry = 5 if V162160 == 1
label variable terror_worry "Worry about Terrorist Attack" 
label define terror_worry 1 "Not worried at all" 2 "Slightly worried" 3 "Moderately worried" 4 "Very worried" 5 "Extremely worried"

gen terror_attack_soon = 0
replace terror_attack_soon = 1 if V162294 == 5
replace terror_attack_soon = 2 if V162294 == 4
replace terror_attack_soon = 3 if V162294 == 3
replace terror_attack_soon = 4 if V162294 == 2
replace terror_attack_soon = 5 if V162294 == 1
label variable terror_attack_soon "How likely terror attack next year" 
label define terror_attack_soon 1 "Not likely at all" 2 "Slightly likely" 3 "Moderately likely" 4 "Very likely" 5 "Extremely likely"


gen leader_c = 0
replace leader_c = 1 if V161159 == 5
replace leader_c = 2 if V161159 == 4
replace leader_c = 3 if V161159 == 3
replace leader_c = 4 if V161159 == 2
replace leader_c = 5 if V161159 == 1
label variable leader_c "Clinton Strong Leadership" 
label define leader_c 1 "Not Well at All" 2 "Slightly Well" 3 "Moderately Well" 4 "Very Well" 5 "Extremely Well" 


gen leader_t = 0
replace leader_t = 1 if V161164 == 5
replace leader_t = 2 if V161164 == 4
replace leader_t = 3 if V161164 == 3
replace leader_t = 4 if V161164 == 2
replace leader_t = 5 if V161164 == 1
label variable leader_t "Trump Strong Leadership" 
label define leader_t 1 "Not Well at All" 2 "Slightly Well" 3 "Moderately Well" 4 "Very Well" 5 "Extremely Well" 


gen leader_diff = (leader_c - leader_t) 
label variable leader_diff "Difference in leadership evals of Clinton and Trump" 

gen knowledgeable_c = 0
replace knowledgeable_c = 1 if V161161 == 5
replace knowledgeable_c = 2 if V161161 == 4
replace knowledgeable_c = 3 if V161161 == 3
replace knowledgeable_c = 4 if V161161 == 2
replace knowledgeable_c = 5 if V161161 == 1
label variable knowledgeable_c "Clinton Knowledgeable" 
label define knowledgeable_c 1 "Not Well at All" 2 "Slightly Well" 3 "Moderately Well" 4 "Very Well" 5 "Extremely Well" 


gen knowledgeable_t = 0
replace knowledgeable_t = 1 if V161166 == 5
replace knowledgeable_t = 2 if V161166 == 4
replace knowledgeable_t = 3 if V161166 == 3
replace knowledgeable_t = 4 if V161166 == 2
replace knowledgeable_t = 5 if V161166 == 1
label variable knowledgeable_t "Trump Knowledgeable" 
label define knowledgeable_t 1 "Not Well at All" 2 "Slightly Well" 3 "Moderately Well" 4 "Very Well" 5 "Extremely Well" 


gen knowledgeable_diff = (leader_c - leader_t) 
label variable knowledgeable_diff "Difference in know. evals of Clinton and Trump" 

gen gender = 0
replace gender = 1  if V161342 == 2
replace gender = . if V161342 == 3
replace gender = . if V161342 == -9
label variable gender "Gender" 
label define gender 0 "Male" 1 "Women" 

gen foreign_secure = .
replace foreign_secure = 1 if V161234 == 1
replace foreign_secure = 2 if V161234 == 3
replace foreign_secure = 3 if V161234 == 2
label define foreign_secure 1 "More secure" 2 "no change" 3 "less secure" 


*** cleaning terrorist variables *** 

foreach var of varlist terror_* party_id race_white education income muslim_* therm_* leader* cares* knowledgeable* honest* speaksmind*{
	replace `var' = . if `var' < = 0
	}

	

	foreach var of varlist V161266* {
	tab `var'
	} 


gen vote_clinton = 0
replace vote_clinton = 1 if V161027 == 1
replace vote_clinton = 1 if V161031 == 1

recode race_white . = 0

gen democrat = 0
replace democrat =1 if party_id <=3
replace democrat = . if party_id == 4



*** Appendix Table 1
eststo terror_therm_c: reg therm_c terror_worry education gender income party_id race_white
eststo terror_therm_t: reg therm_t terror_worry education gender income party_id race_white
eststo terror_leader_c: reg leader_c terror_worry education gender income party_id race_white
eststo terror_leader_t: reg leader_t terror_worry education gender income party_id race_white

esttab using Table1.rtf, replace nogap se b(%9.2f) starlevels(^ .10 * .05 ** .01 *** .001) r2(%9.2f) ///
title("Appendix Table 1") label

eststo clear

*** Figure 1 (with the combomaginsplot package)

* if necessary, install combomarginsplot here 
*ssc install combomarginsplot

reg therm_c terror_worry education gender income party_id race_white
margins, at(terror_worry=(1(1)5)) atmeans vsquish saving(file1, replace)
reg therm_t terror_worry education gender income party_id race_white
margins, at(terror_worry=(1(1)5)) atmeans vsquish saving(file2, replace)
combomarginsplot file1 file2, ///
labels("Clinton" "Trump") ytitle ("Feeling thermometer") title("") legend (rows(1) pos(6))
graph save "Figure1a.gph", replace
graph export "Figure1a.png", replace width(1800)

reg leader_c terror_worry education gender income party_id race_white
margins, at(terror_worry=(1(1)5)) atmeans vsquish saving(file3, replace)
reg leader_t terror_worry education gender income party_id race_white
margins, at(terror_worry=(1(1)5)) atmeans vsquish saving(file4, replace)
combomarginsplot file3 file4, ///
labels("Clinton" "Trump") ytitle("Leadership Evaluation") title("") legend (off)
graph save "Figure1b.gph", replace
graph export "Figure1b.png", replace width(1800)


grc1leg  "Figure1a" "Figure1b",  graphregion(margin(none)) row(1) ysize(2.5) 
graph save Figure1.gph, replace
graph export Figure1.png, replace width(1800)


*** Appendix Table 2
eststo attack_therm_c: reg therm_c terror_attack_soon education gender income party_id race_white
eststo attack_therm_t: reg therm_t terror_attack_soon education gender income party_id race_white
eststo attack_leader_c: ologit leader_c terror_attack_soon education gender income party_id race_white
eststo attack_leader_t: ologit leader_t terror_attack_soon education gender income party_id race_white

esttab using Table2.rtf, replace nogap se b(%9.2f) starlevels(^ .10 * .05 ** .01 *** .001) r2(%9.2f) ///
title("Appendix Table 2") label

eststo clear

*** Appendix Table 3
eststo terror_therm_c: reg therm_c terror_worry education gender income party_id race_white
eststo terror_therm_t: reg therm_t terror_worry education gender income party_id race_white
eststo terror_leader_c: ologit leader_c terror_worry education gender income party_id race_white
eststo terror_leader_t: ologit leader_t terror_worry education gender income party_id race_white

esttab using Table3.rtf, replace nogap se b(%9.2f) starlevels(^ .10 * .05 ** .01 *** .001) r2(%9.2f) ///
title("Appendix Table 3") label

eststo clear


